package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.BrandService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.CategoryBrandRelationDao;
import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * @author ShiJieLin
 */
@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Resource
    private CategoryService categoryService;
    @Resource
    private BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {

        // 1.查询品牌的名字
        QueryWrapper<BrandEntity> brandWrapper = new QueryWrapper<>();
        brandWrapper.eq("brand_id", categoryBrandRelation.getBrandId());
        brandWrapper.select("name");

        BrandEntity brand = brandService.getOne(brandWrapper);
        // 2.查询分类的名字
        QueryWrapper<CategoryEntity> categoryWrapper = new QueryWrapper<>();
        categoryWrapper.eq("cat_id", categoryBrandRelation.getCatelogId());
        categoryWrapper.select("name");

        CategoryEntity category = categoryService.getOne(categoryWrapper);
        // 3.保存关联的名字信息
        categoryBrandRelation.setBrandName(brand.getName());
        categoryBrandRelation.setCatelogName(category.getName());

        baseMapper.insert(categoryBrandRelation);
    }

    @Override
    public void updateBrand(Long brandId, String name) {

        CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
        relationEntity.setBrandId(brandId);
        relationEntity.setBrandName(name);
        this.update(relationEntity,
                new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
    }

    @Override
    public void updateCategory(Long catId, String CategoryName) {

        baseMapper.updateCategory(catId, CategoryName);
    }

    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        // 1.查询关联表中该三级分类下 关联的 品牌id
        QueryWrapper<CategoryBrandRelationEntity> cBRelationWrapper = new QueryWrapper<>();
        cBRelationWrapper.eq("catelog_id", catId);
        cBRelationWrapper.select("brand_id");
        List<CategoryBrandRelationEntity> relationEntities = this.list(cBRelationWrapper);
        // 2.获取三级分类下 所有品牌id
        List<Long> brandIds = relationEntities.stream().map((relationEntity) ->
                relationEntity.getBrandId()).collect(Collectors.toList());

        if(null != brandIds && brandIds.size() > 0){
            Collection<BrandEntity> brandEntities = brandService.listByIds(brandIds);

            return (List<BrandEntity>) brandEntities;
        }
        return null;
    }

}